<?php
/**
 * ClassName: profit_share
 * Description: 微信商户分账控制器
 * @Author 周昌
 * @Create 2023/11/8 13:33
 */

namespace app;


use epii\admin\center\config\Settings;
use epii\server\Args;
use libs\Tool;
use model\ProfitShareModel;
use service\admin\ProfitShareService;

class profit_share extends base
{
    public function init()
    {
        parent::init();
        $this->accessDenied();
    }

    public function index()
    {
        //筛选
        $this->adminUiDisplay();
    }

    public function ajax_data()
    {
        $map = [];
        $name = Args::params('name');
        if ($name) {
            $map[] = ['name', "LIKE", '%' . $name . '%'];
        }
        $account = Args::params('account');
        if ($account) {
            $map[] = ['account', "=",$account];
        }
        $type = Args::params('type');
        if ($type) {
            $map[] = ['type', "=",$type];
        }
        echo ProfitShareService::getAjaxData($map);
    }

    public function add_edit()
    {
        $id = Args::params('id');
        if ($_SERVER['REQUEST_METHOD'] == "POST") {
            $data = Args::params('r');
            //验证
            $this->validate($data, [
                'name|账户名称' => 'require|max:30',
//                'type|分账类型' => 'require|integer',
                'status|状态' => 'require|integer',
                'account|账户号' => 'require|alphaNum|max:20',
//                'amount|分账比例' => 'float|>=:0|<=:1',
                'description|描述' => 'max:255',
            ]);
            if ($id) {
                //判断分账总比例是否大于1
//                $result=ProfitShareService::shareRatio($data['amount'],[['id','<>',$id]]);
//                if($result['code']==0){
//                    $this->error($result['msg']);
//                }
                $res = ProfitShareService::edit($id, $data);
            } else {
                //判断分账总比例是否大于1
//                $result=ProfitShareService::shareRatio($data['amount']);
//                if($result['code']==0){
//                    $this->error($result['msg']);
//                }
                $data['hospital_group_id']=$this->adminInfo['hospital_group_id'];
                $data['hospital_id']=$this->adminInfo['hospital_id'];
                $res = ProfitShareService::add($data);
            }
            $res['code'] ? $this->success($res['msg']) : $this->error($res['msg']);
        }
        $this->_as_info = $id ? ProfitShareModel::getInfo(['id' => $id]) : [];
        $this->_as_id = $id;
        $this->_as_status = ProfitShareService::statusSelect();

        $this->adminUiDisplay();
    }

    //修改状态
    public function status()
    {
        $id = Args::params('id/d/1', 'id,不能为空');
        $status = Args::params('status');
//        if ($status==ProfitShareService::$status_index['yes']){
//            //启用的时候判断分账总比例是否大于1
//            $amount=ProfitShareModel::getValue(['id'=>$id],'amount');
//            $result=ProfitShareService::shareRatio($amount,[['id','<>',$id]]);
//            if($result['code']==0){
//                $this->error($result['msg']);
//            }
//        }
        $res = ProfitShareService::status($id, $status);
        if ($res['code']) {
            $this->success('成功', 'refresh');
        }
        $this->error();
    }

    /**
     * @return array|false|string
     * @throws \think\Exception
     * @throws \think\db\exception\PDOException
     * 删除
     */
    public function del()
    {
        $id = Args::params('id');
        $res = ProfitShareService::del($id);
        if ($res['code']) {
            $this->success($res['msg'], 'refresh');
        } else {
            $this->error($res['msg']);
        }
    }
}